package com.handydata.antifraud.ws.dap;

import com.alibaba.fastjson.JSONObject;
import com.handydata.antifraud.annotation.Comment;
import com.handydata.antifraud.common.constant.DataSourceCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.to.dap.HuaDaoMultiReqTO;
import com.handydata.antifraud.to.dap.HuaDaoMultiResTO;
import com.handydata.antifraud.util.DateUtil;
import com.handydata.antifraud.util.encrypt.Md5Utils;
import com.handydata.antifraud.ws.WsInvocationService;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

import java.util.Date;

/**
 * 华道信贷多头
 *
 */
@Slf4j
@Service("huaDaoMultiService")
@Comment("华道信贷多头")
public class HuaDaoMultiWsServiceImpl extends DataPlatformWsService<HuaDaoMultiReqTO, HuaDaoMultiResTO> implements
		WsInvocationService<HuaDaoMultiReqTO, HuaDaoMultiResTO> {

	@Override
	public HuaDaoMultiResTO query(HuaDaoMultiReqTO req) throws WsInvokeException {
		log.info("华道信贷多头查询请求参数 {}", req);
		HuaDaoMultiResTO res = new HuaDaoMultiResTO();
		if (StringUtils.isEmpty(req.getPhoneNum())) {
			log.error("华道信贷多头查询参数校验 {}", "phoneNum为空");
			res.setResCode("1111");
			res.setResMsg("华道信贷多头查询接口phoneNum参数为空");
			return res;
		}
		if (StringUtils.isEmpty(req.getName())) {
			log.error("华道信贷多头查询参数校验 {}", "name为空");
			res.setResCode("1111");
			res.setResMsg("华道信贷多头查询接口name参数为空");
			return res;
		}
		if (StringUtils.isEmpty(req.getIdCard())) {
			log.error("华道信贷多头查询参数校验 {}", "idCard为空");
			res.setResCode("1111");
			res.setResMsg("华道信贷多头查询接口idCard参数为空");
			return res;
		}
		this.dataType = DataSourceCode.HUA_DAO_MULTI.getCode();
		try {
			res = executeQuery(req);
		} catch (Exception e) {
			res.setResCode("1111");
			res.setResMsg("执行外部数据源异常");
		}
		return res;
	}

	@Override
	protected String createQueryXml(HuaDaoMultiReqTO req) {
		StringBuilder xml = new StringBuilder();
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
		xml.append("<subatm>");
		xml.append("<application>GwBiz.Req</application>");
		xml.append("<version>2.0.0</version>");
		xml.append("<sendTime>").append(DateUtil.now14()).append("</sendTime>");
		xml.append("<transCode>300683</transCode>");
		xml.append("<channelId>").append(encryInfo.getChannelId()).append("</channelId>");
		xml.append("<channelOrderId>").append(DateUtil.now17()).append("</channelOrderId>");
		xml.append("<cid>").append(Md5Utils.md5ToHexStr(req.getIdCard())).append("</cid>");
		xml.append("<name>").append(Md5Utils.md5ToHexStr(req.getName())).append("</name>");
		xml.append("<phone>").append(Md5Utils.md5ToHexStr(req.getPhoneNum())).append("</phone>");
		xml.append("</subatm>");
		log.info("华道信贷多头查询调用翰迪数据平台请求报文{}", xml);
		req.setQueryXml(xml.toString());
		return xml.toString();
	}

	@Override
	protected HuaDaoMultiResTO parseResultAndSave(String resultXml,	String orderId, Date createDate) {
		log.info("调用华道信贷多头查询接口返回报文：{}", resultXml);
		HuaDaoMultiResTO resTO = new HuaDaoMultiResTO();
		try {
			// 解析xml，保存本地DB
			JSONObject jasonObject = JSONObject.parseObject(resultXml);
			if (null != jasonObject) {
				if ("0000".equals(jasonObject.get("resCode"))
						&& StringUtils.isNotBlank(jasonObject.getString("resData"))) {
					JSONObject data = JSONObject.parseObject(jasonObject.getString("resData"));
					if ("1".equals(data.getString("exists"))) {
						handResult(data.getString("data"), resTO);
					}
				}
				resTO.setResCode((String) jasonObject.get("resCode"));
				resTO.setResMsg((String) jasonObject.get("resMsg"));
			} else {
				resTO.setResCode("0001");
				resTO.setResMsg("华道信贷多头查询接口返回失败");
			}
		} catch (Exception e) {
			log.info("调用华道信贷多头查询接口返回报文解析异常：{}", e);
			resTO.setResCode("1111");
			resTO.setResMsg("调用华道信贷多头查询接口异常");
		}
		log.info("调用华道信贷多头查询接口返回结果：{}", JSONObject.toJSONString(resTO));
		return resTO;
	}

	@SuppressWarnings("unchecked")
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, HuaDaoMultiReqTO.class);
	}
	
	/**
	 * 处理返回结果
	 */
	private void handResult (String resData, HuaDaoMultiResTO resTO) {
		JSONObject data = JSONObject.parseObject(resData);
		if (StringUtils.isNotBlank(data.getString("D157"))) {
			resTO.setHdMultiD157(data.getString("D157"));
		}
		if (StringUtils.isNotBlank(data.getString("D158"))) {
			resTO.setHdMultiD158(data.getString("D158"));
		}
		if (StringUtils.isNotBlank(data.getString("D159"))) {
			resTO.setHdMultiD159(data.getString("D159"));
		}
		if (StringUtils.isNotBlank(data.getString("D384"))) {
			resTO.setHdMultiD384(data.getString("D384"));
		}
		if (StringUtils.isNotBlank(data.getString("D385"))) {
			resTO.setHdMultiD385(data.getString("D385"));
		}
		if (StringUtils.isNotBlank(data.getString("D430"))) {
			resTO.setHdMultiD430(data.getString("D430"));
		}
		if (StringUtils.isNotBlank(data.getString("D437"))) {
			resTO.setHdMultiD437(data.getString("D437"));
		}
		if (StringUtils.isNotBlank(data.getString("D156"))) {
			resTO.setHdMultiD156(data.getString("D156"));
		}
		if (StringUtils.isNotBlank(data.getString("D434"))) {
			resTO.setHdMultiD434(data.getString("D434"));
		}
		if (StringUtils.isNotBlank(data.getString("D027"))) {
			resTO.setHdMultiD027(data.getString("D027"));
		}
		if (StringUtils.isNotBlank(data.getString("D028"))) {
			resTO.setHdMultiD028(data.getString("D028"));
		}
		if (StringUtils.isNotBlank(data.getString("D273"))) {
			resTO.setHdMultiD273(data.getString("D273"));
		}
		if (StringUtils.isNotBlank(data.getString("D035"))) {
			resTO.setHdMultiD035(data.getString("D035"));
		}
		if (StringUtils.isNotBlank(data.getString("D039"))) {
			resTO.setHdMultiD039(data.getString("D039"));
		}
		if (StringUtils.isNotBlank(data.getString("D228"))) {
			resTO.setHdMultiD228(data.getString("D228"));
		}
		if (StringUtils.isNotBlank(data.getString("D047"))) {
			resTO.setHdMultiD047(data.getString("D047"));
		}
		if (StringUtils.isNotBlank(data.getString("D048"))) {
			resTO.setHdMultiD048(data.getString("D048"));
		}
		if (StringUtils.isNotBlank(data.getString("D049"))) {
			resTO.setHdMultiD049(data.getString("D049"));
		}
		if (StringUtils.isNotBlank(data.getString("D291"))) {
			resTO.setHdMultiD291(data.getString("D291"));
		}
		if (StringUtils.isNotBlank(data.getString("D081"))) {
			resTO.setHdMultiD081(data.getString("D081"));
		}
		if (StringUtils.isNotBlank(data.getString("D082"))) {
			resTO.setHdMultiD082(data.getString("D082"));
		}
		if (StringUtils.isNotBlank(data.getString("D318"))) {
			resTO.setHdMultiD318(data.getString("D318"));
		}
		if (StringUtils.isNotBlank(data.getString("D085"))) {
			resTO.setHdMultiD085(data.getString("D085"));
		}
		if (StringUtils.isNotBlank(data.getString("D089"))) {
			resTO.setHdMultiD089(data.getString("D089"));
		}
		if (StringUtils.isNotBlank(data.getString("D093"))) {
			resTO.setHdMultiD093(data.getString("D093"));
		}
		if (StringUtils.isNotBlank(data.getString("D236"))) {
			resTO.setHdMultiD236(data.getString("D236"));
		}
		if (StringUtils.isNotBlank(data.getString("D101"))) {
			resTO.setHdMultiD101(data.getString("D101"));
		}
		if (StringUtils.isNotBlank(data.getString("D131"))) {
			resTO.setHdMultiD131(data.getString("D131"));
		}
		if (StringUtils.isNotBlank(data.getString("D135"))) {
			resTO.setHdMultiD135(data.getString("D135"));
		}
		if (StringUtils.isNotBlank(data.getString("D139"))) {
			resTO.setHdMultiD139(data.getString("D139"));
		}
		if (StringUtils.isNotBlank(data.getString("D143"))) {
			resTO.setHdMultiD143(data.getString("D143"));
		}
		if (StringUtils.isNotBlank(data.getString("D244"))) {
			resTO.setHdMultiD244(data.getString("D244"));
		}
		if (StringUtils.isNotBlank(data.getString("D178"))) {
			resTO.setHdMultiD178(data.getString("D178"));
		}
		if (StringUtils.isNotBlank(data.getString("D179"))) {
			resTO.setHdMultiD179(data.getString("D179"));
		}
		if (StringUtils.isNotBlank(data.getString("D180"))) {
			resTO.setHdMultiD180(data.getString("D180"));
		}
		if (StringUtils.isNotBlank(data.getString("D402"))) {
			resTO.setHdMultiD402(data.getString("D402"));
		}
		if (StringUtils.isNotBlank(data.getString("D193"))) {
			resTO.setHdMultiD193(data.getString("D193"));
		}
		if (StringUtils.isNotBlank(data.getString("D194"))) {
			resTO.setHdMultiD194(data.getString("D194"));
		}
		if (StringUtils.isNotBlank(data.getString("D195"))) {
			resTO.setHdMultiD195(data.getString("D195"));
		}
		if (StringUtils.isNotBlank(data.getString("D208"))) {
			resTO.setHdMultiD208(data.getString("D208"));
		}
		if (StringUtils.isNotBlank(data.getString("D207"))) {
			resTO.setHdMultiD207(data.getString("D207"));
		}
		if (StringUtils.isNotBlank(data.getString("D209"))) {
			resTO.setHdMultiD209(data.getString("D209"));
		}
		if (StringUtils.isNotBlank(data.getString("D219"))) {
			resTO.setHdMultiD219(data.getString("D219"));
		}
		if (StringUtils.isNotBlank(data.getString("D220"))) {
			resTO.setHdMultiD220(data.getString("D220"));
		}
		if (StringUtils.isNotBlank(data.getString("D221"))) {
			resTO.setHdMultiD221(data.getString("D221"));
		}
		if (StringUtils.isNotBlank(data.getString("D414"))) {
			resTO.setHdMultiD414(data.getString("D414"));
		}
		if (StringUtils.isNotBlank(data.getString("D050"))) {
			resTO.setHdMultiD050(data.getString("D050"));
		}
		if (StringUtils.isNotBlank(data.getString("D057"))) {
			resTO.setHdMultiD057(data.getString("D057"));
		}
		if (StringUtils.isNotBlank(data.getString("D059"))) {
			resTO.setHdMultiD059(data.getString("D059"));
		}
		if (StringUtils.isNotBlank(data.getString("D297"))) {
			resTO.setHdMultiD297(data.getString("D297"));
		}
	}
	
}
	
